﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Xsl;
using System.Xml.Linq;

using System.Collections.ObjectModel;

using EzDesk.Utility;

namespace EzDesk.Client.Utility
{
    /// @Refactorying with Interface to instead of the class
    /// <summary>
    /// 
    /// </summary>
    public class FeedCollection : List<FeedClientModel>
    {
        private string _xmlData = string.Empty;

        private IList<KeyValuePair<string, string>> _parameters = new List<KeyValuePair<string, string>>();

        public static FeedCollection Create(string xmlData)
        {
            return FeedCollection.Create(xmlData, "html");
        }

        public static FeedCollection Create(string xmlData, string targetType)
        {
            var feedCollection = new FeedCollection(xmlData);
            FeedTransformBase feedTransform = FeedCollection.CreateTransform(targetType);
            feedTransform.Transform(feedCollection, feedCollection._parameters.ToArray());

            return feedCollection;
        }

        private static FeedTransformBase CreateTransform(string targetType)
        {
            FeedTransformBase feedTransform;
            switch (targetType)
            {
                case "xaml":
                    feedTransform= new XamlFeedTransform();
                    break;
                case "html":
                    feedTransform= new HtmlFeedTransform();
                    break;
                default:
                    feedTransform= new HtmlFeedTransform();
                    break;
            }
            return feedTransform;
        }

        public FeedCollection()
        { }

        private FeedCollection(string xmlData)
            : base()
        {
            _xmlData = xmlData;

            XElement xelement = XElement.Parse(xmlData);

            _parameters.Add(new KeyValuePair<string, string>(
                "appportalurl",
                xelement.Attribute("appportalurl").Value
                )
                );

            var feeds = xelement
                .Element("feeditems")
                .Elements("feeditem")
                .Foreach(item =>
                    this.Add(BuildFeedModel(item))
                    );

        }
        private FeedClientModel BuildFeedModel(XElement xmlData)
        {
            FeedClientModel model = new FeedClientModel();
            model.AppName = xmlData.Attribute("appname").Value;
            model.FeedId = new Guid(xmlData.Attribute("guid").Value);
            model.CreateOn = Convert.ToDateTime(xmlData.Attribute("createon").Value);
            model.UpdateOn = Convert.ToDateTime(xmlData.Attribute("updateon").Value);
            model.LastChangeOn = Convert.ToDateTime(xmlData.Attribute("lastchangeon").Value);
            model.IsAvailable = Convert.ToBoolean(xmlData.Attribute("isavailable").Value);
            model.IsRead = Convert.ToBoolean(xmlData.Attribute("isread").Value);
            model.Type = xmlData.Attribute("type").Value;
            model.ExtensionData = JsonHelper.JsonToDictionary(xmlData.Element("data").Value).ToList();
            model.OriginalData = DesignFix(xmlData.Element("content").Value);
            return model;
        }
        //@Refactory [These 2 lines should be move to Class: XamlFeedTransform]
        private const string _xmlRootTemplate = "<root>{0}</root>";
        private string DesignFix(string str)
        {
            return string.Format(_xmlRootTemplate, str);
        }

     
        public static FeedCollection Create(IEnumerable<FeedClientModel> feeds)
        {
            FeedCollection feedCollection = new FeedCollection();
            feedCollection.AddRange(feeds);
            return feedCollection;
        }
    }

}
